package algotithm.leetcode.listnode.test379;

import java.util.Arrays;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/22-22:50
 * @URL https://leetcode-cn.com/problems/design-phone-directory/
 * @Title 379. 电话目录管理系统
 */
class PhoneDirectory {

    /** Initialize your data structure here
     @param maxNumbers - The maximum numbers that can be stored in the phone directory. */
    private boolean[] phone;
    private int size;

    public PhoneDirectory(int maxNumbers) {
        this.size = maxNumbers;
        phone = new boolean[maxNumbers];
        // true 表示可以使用
        Arrays.fill(phone, true);
    }

    /**
     * 分配给用户一个未被使用的电话号码，获取失败请返回 -1.
     */
    public int get() {
        for (int i = 0; i < size; i++) {
            if (phone[i]) {
                phone[i] = false;
                return i;
            }
        }
        return -1;
    }

    /**
     * 检查指定的电话号码是否被使用.
     */
    public boolean check(int number) {
        return phone[number];
    }

    /**
     * 释放掉一个电话号码，使其能够重新被分配.
     */
    public void release(int number) {
        phone[number] = true;
    }
}

/**
 * Your PhoneDirectory object will be instantiated and called as such:
 * PhoneDirectory obj = new PhoneDirectory(maxNumbers);
 * int param_1 = obj.get();
 * boolean param_2 = obj.check(number);
 * obj.release(number);
 */


